package com.lebang.medicalInquiry.controller;


import com.lebang.medicalInquiry.constant.ResponseEnum;
import com.lebang.medicalInquiry.constant.annotation.RateLimit;
import com.lebang.medicalInquiry.constant.annotation.Require;
import com.lebang.medicalInquiry.model.po.OffLineNotice;
import com.lebang.medicalInquiry.model.vo.PageVo;
import com.lebang.medicalInquiry.model.vo.UserTokenVo;
import com.lebang.medicalInquiry.response.Callback;
import com.lebang.medicalInquiry.response.JsonData;
import com.lebang.medicalInquiry.service.NoticeService;
import com.lebang.medicalInquiry.utils.shengwang.RtcTokenBuilder2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @version :1.0
 * @date ：Created in 2022/3/10 11:32
 */
@Api(tags = "消息相关")
@RequestMapping("/api/notice")
@RestController
@Validated
public class NoticeApi {

    @Autowired
    private NoticeService noticeService;

    @Value("${sw.app_id}")
    private String appId;

    @Value("${sw.app_certificate}")
    private String appCertificate;

    @GetMapping("/")
    @Require
    @ApiOperation("查询离线消息")
    public JsonData<PageVo<List<OffLineNotice>>> getOffLineNotice(HttpServletRequest request, HttpServletResponse response,
                                                                  @ApiParam(name = "chatUuid", value = "被聊天的uuid", required = true) @NotNull(message = "chatUuid 不能为空")
                                                                  @RequestParam(value = "chatUuid", required = false) String chatUuid,
                                                                  @ApiParam(name = "page", value = "页码")
                                                                  @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                                                  @ApiParam(name = "limit", value = "条数")
                                                                  @RequestParam(value = "limit", required = false, defaultValue = "1") Integer limit) {
        String uuid = request.getAttribute("uuid").toString();
        PageVo<List<OffLineNotice>> list = noticeService.getOffLineNotice(uuid, chatUuid, page, limit);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), list);

    }

//    @Require
    @GetMapping("/sy/token")
    @ApiOperation("声网语音通话token获取")
    public JsonData liveChat(HttpServletRequest request, HttpServletResponse response,
                             @ApiParam(name = "uid", value = "uid") @NotNull(message = "uid 必传")
                             @RequestParam(value = "uid", defaultValue = "1", required = false) Integer uid,
                             @ApiParam(name = "channelName", value = "channelName") @NotBlank(message = "channelName 必传")
                             @RequestParam(value = "channelName", defaultValue = "1", required = false) String channelName) {

        int tokenExpirationInSeconds = 3600;
        int privilegeExpirationInSeconds = 1800;
        RtcTokenBuilder2 token = new RtcTokenBuilder2();
        String result = token.buildTokenWithUid(appId, appCertificate, channelName, uid, RtcTokenBuilder2.Role.ROLE_PUBLISHER,
                tokenExpirationInSeconds, privilegeExpirationInSeconds);

        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), result);

    }
}
